from pyecharts import options as opts
from pyecharts.charts import Graph

# 节点列表，格式为{"name": "节点名称", "category": "类别"}
nodes = [
    {"name": "工业增加值", "category": "宏观经济指标"},
    {"name": "社零", "category": "宏观经济指标"},
    {"name": "固定资产投资", "category": "宏观经济指标"},
    {"name": "出口", "category": "宏观经济指标"},
    {"name": "GDP", "category": "宏观经济指标"},
    {"name": "汽车产量", "category": "行业数据"},
    {"name": "工业机器人产量", "category": "行业数据"},
    {"name": "发电设备产量", "category": "行业数据"},
    {"name": "智能手机产量", "category": "行业数据"},
    {"name": "集成电路产量", "category": "行业数据"},
    {"name": "太阳能电池产量", "category": "行业数据"},
    {"name": "水泥产量", "category": "行业数据"},
    {"name": "通讯器材零售", "category": "行业数据"},
    {"name": "家电零售", "category": "行业数据"},
    {"name": "体育娱乐用品", "category": "行业数据"},
    {"name": "烟酒零售", "category": "行业数据"},
    {"name": "汽车零售", "category": "行业数据"},
    {"name": "家具零售", "category": "行业数据"},
    {"name": "设备工器具投资", "category": "行业数据"},
    {"name": "房地产开发投资", "category": "行业数据"},
    {"name": "基建投资", "category": "行业数据"},
    {"name": "制造业投资", "category": "行业数据"},
    {"name": "房地产销售", "category": "行业数据"},
    {"name": "房价", "category": "行业数据"},
    {"name": "以旧换新政策", "category": "政策"},
    {"name": "设备更新政策", "category": "政策"},
    {"name": "化债类地方债", "category": "政策"},
    {"name": "提振消费专项行动方案", "category": "政策"},
    {"name": "股票市场", "category": "资产配置"},
    {"name": "固收市场", "category": "资产配置"},
    {"name": "大宗商品", "category": "资产配置"},
    {"name": "银行", "category": "行业分析"},
    {"name": "地产", "category": "行业分析"},
    {"name": "旅游酒店及餐饮", "category": "行业分析"},
    {"name": "轻工零售美妆", "category": "行业分析"},
    {"name": "建筑建材", "category": "行业分析"},
    {"name": "食品饮料", "category": "行业分析"},
    {"name": "农业", "category": "行业分析"},
    {"name": "内需改善", "category": "经济趋势"},
    {"name": "政策红利", "category": "经济趋势"},
    {"name": "房地产趋稳", "category": "经济趋势"},
    {"name": "消费分化", "category": "经济趋势"},
    {"name": "出口压力", "category": "经济趋势"},
    {"name": "广发证券", "category": "机构"},
    {"name": "中金公司", "category": "机构"},
    {"name": "中信证券", "category": "机构"}
]

# 边列表，格式为{"source": "源节点", "target": "目标节点", "symbolSize": 权重}
edges = [
    {"source": "广发证券", "target": "工业增加值", "symbolSize": 5},
    {"source": "广发证券", "target": "社零", "symbolSize": 5},
    {"source": "广发证券", "target": "固定资产投资", "symbolSize": 5},
    {"source": "广发证券", "target": "出口", "symbolSize": 5},
    {"source": "广发证券", "target": "GDP", "symbolSize": 5},
    {"source": "广发证券", "target": "汽车产量", "symbolSize": 5},
    {"source": "广发证券", "target": "工业机器人产量", "symbolSize": 5},
    {"source": "广发证券", "target": "发电设备产量", "symbolSize": 5},
    {"source": "广发证券", "target": "智能手机产量", "symbolSize": 5},
    {"source": "广发证券", "target": "集成电路产量", "symbolSize": 5},
    {"source": "广发证券", "target": "太阳能电池产量", "symbolSize": 5},
    {"source": "广发证券", "target": "水泥产量", "symbolSize": 5},
    {"source": "广发证券", "target": "通讯器材零售", "symbolSize": 5},
    {"source": "广发证券", "target": "家电零售", "symbolSize": 5},
    {"source": "广发证券", "target": "体育娱乐用品", "symbolSize": 5},
    {"source": "广发证券", "target": "烟酒零售", "symbolSize": 5},
    {"source": "广发证券", "target": "汽车零售", "symbolSize": 5},
    {"source": "广发证券", "target": "家具零售", "symbolSize": 5},
    {"source": "广发证券", "target": "设备工器具投资", "symbolSize": 5},
    {"source": "广发证券", "target": "房地产开发投资", "symbolSize": 5},
    {"source": "广发证券", "target": "基建投资", "symbolSize": 5},
    {"source": "广发证券", "target": "制造业投资", "symbolSize": 5},
    {"source": "广发证券", "target": "房地产销售", "symbolSize": 5},
    {"source": "广发证券", "target": "房价", "symbolSize": 5},
    {"source": "广发证券", "target": "以旧换新政策", "symbolSize": 5},
    {"source": "广发证券", "target": "设备更新政策", "symbolSize": 5},
    {"source": "广发证券", "target": "化债类地方债", "symbolSize": 5},
    {"source": "广发证券", "target": "内需改善", "symbolSize": 5},
    {"source": "广发证券", "target": "政策红利", "symbolSize": 5},
    {"source": "广发证券", "target": "房地产趋稳", "symbolSize": 5},
    {"source": "广发证券", "target": "消费分化", "symbolSize": 5},
    {"source": "广发证券", "target": "出口压力", "symbolSize": 5},
    {"source": "中金公司", "target": "工业增加值", "symbolSize": 5},
    {"source": "中金公司", "target": "社零", "symbolSize": 5},
    {"source": "中金公司", "target": "固定资产投资", "symbolSize": 5},
    {"source": "中金公司", "target": "出口", "symbolSize": 5},
    {"source": "中金公司", "target": "GDP", "symbolSize": 5},
    {"source": "中金公司", "target": "通讯器材零售", "symbolSize": 5},
    {"source": "中金公司", "target": "家电零售", "symbolSize": 5},
    {"source": "中金公司", "target": "体育娱乐用品", "symbolSize": 5},
    {"source": "中金公司", "target": "汽车零售", "symbolSize": 5},
    {"source": "中金公司", "target": "家具零售", "symbolSize": 5},
    {"source": "中金公司", "target": "设备工器具投资", "symbolSize": 5},
    {"source": "中金公司", "target": "房地产开发投资", "symbolSize": 5},
    {"source": "中金公司", "target": "基建投资", "symbolSize": 5},
    {"source": "中金公司", "target": "制造业投资", "symbolSize": 5},
    {"source": "中金公司", "target": "房地产销售", "symbolSize": 5},
    {"source": "中金公司", "target": "房价", "symbolSize": 5},
    {"source": "中金公司", "target": "以旧换新政策", "symbolSize": 5},
    {"source": "中金公司", "target": "提振消费专项行动方案", "symbolSize": 5},
    {"source": "中金公司", "target": "股票市场", "symbolSize": 5},
    {"source": "中金公司", "target": "固收市场", "symbolSize": 5},
    {"source": "中金公司", "target": "大宗商品", "symbolSize": 5},
    {"source": "中金公司", "target": "银行", "symbolSize": 5},
    {"source": "中金公司", "target": "地产", "symbolSize": 5},
    {"source": "中金公司", "target": "旅游酒店及餐饮", "symbolSize": 5},
    {"source": "中金公司", "target": "轻工零售美妆", "symbolSize": 5},
    {"source": "中金公司", "target": "建筑建材", "symbolSize": 5},
    {"source": "中金公司", "target": "食品饮料", "symbolSize": 5},
    {"source": "中金公司", "target": "农业", "symbolSize": 5},
    {"source": "中金公司", "target": "内需改善", "symbolSize": 5},
    {"source": "中金公司", "target": "政策红利", "symbolSize": 5},
    {"source": "中金公司", "target": "房地产趋稳", "symbolSize": 5},
    {"source": "中金公司", "target": "消费分化", "symbolSize": 5},
    {"source": "中金公司", "target": "出口压力", "symbolSize": 5},
    {"source": "中信证券", "target": "工业增加值", "symbolSize": 5},
    {"source": "中信证券", "target": "社零", "symbolSize": 5},
    {"source": "中信证券", "target": "固定资产投资", "symbolSize": 5},
    {"source": "中信证券", "target": "出口", "symbolSize": 5},
    {"source": "中信证券", "target": "GDP", "symbolSize": 5},
    {"source": "中信证券", "target": "通讯器材零售", "symbolSize": 5},
    {"source": "中信证券", "target": "家电零售", "symbolSize": 5},
    {"source": "中信证券", "target": "体育娱乐用品", "symbolSize": 5},
    {"source": "中信证券", "target": "汽车零售", "symbolSize": 5},
    {"source": "中信证券", "target": "家具零售", "symbolSize": 5},
    {"source": "中信证券", "target": "设备工器具投资", "symbolSize": 5},
    {"source": "中信证券", "target": "房地产开发投资", "symbolSize": 5},
    {"source": "中信证券", "target": "基建投资", "symbolSize": 5},
    {"source": "中信证券", "target": "制造业投资", "symbolSize": 5},
    {"source": "中信证券", "target": "房地产销售", "symbolSize": 5},
    {"source": "中信证券", "target": "房价", "symbolSize": 5},
    {"source": "中信证券", "target": "以旧换新政策", "symbolSize": 5},
    {"source": "中信证券", "target": "提振消费专项行动方案", "symbolSize": 5},
    {"source": "中信证券", "target": "内需改善", "symbolSize": 5},
    {"source": "中信证券", "target": "政策红利", "symbolSize": 5},
    {"source": "中信证券", "target": "房地产趋稳", "symbolSize": 5},
    {"source": "中信证券", "target": "消费分化", "symbolSize": 5},
    {"source": "中信证券", "target": "出口压力", "symbolSize": 5}
]

# 类别列表，格式为{"name": "类别名称"}
categories = [{"name": "宏观经济指标"}, {"name": "行业数据"}, {"name": "政策"}, {"name": "资产配置"}, {"name": "行业分析"}, {"name": "经济趋势"}, {"name": "机构"}]

# 创建关系图
c = (
    Graph()
    .add(
        series_name="",
        nodes=nodes,
        links=edges,
        categories=categories,
        layout="force",
        repulsion=5000,
        edge_length=50,
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="2025年1-2月经济数据知识图谱"),
        legend_opts=opts.LegendOpts(is_show=True),
        # 设置宽度和高度
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        # 以下设置宽度为 1200px，高度为 800px
        graphic_opts=[opts.GraphicGroup(
            graphic_item=opts.GraphicItem(
                bounding="raw",
                left="center",
                top="center",
                z=-100,
            ),
            children=[
                opts.GraphicRect(
                    graphic_item=opts.GraphicItem(
                        left="center",
                        top="center",
                        z=-100,
                    ),
                    graphic_shape_opts=opts.GraphicShapeOpts(
                        width=1600,
                        height=1200
                    ),
                    graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                        fill="rgba(255,255,255,0)"
                    )
                )
            ]
        )]
    )
)

# 渲染图表
c.render("三月经济分析.html")